<script lang="ts" setup>
import { ref } from 'vue'
import { user, logout } from '~/composables/useAuth'

// 注销确认对话框状态
const logoutDialogVisible = ref(false)

const handleLogout = () => {
  logoutDialogVisible.value = true
}

const confirmLogout = () => {
  logout()
  logoutDialogVisible.value = false
}

const cancelLogout = () => {
  logoutDialogVisible.value = false
}

// 新增：判断是否显示数据管理菜单
const showDataManagement = () => {
  return user.value && user.value.level === 2
}
</script>

<template>
  <el-menu class="el-menu-demo" mode="horizontal"
           text-color="black"
           active-text-color="#13675b"
           :ellipsis="false" router>

    <!-- Logo和标题部分 -->
    <el-menu-item index="/" class="logo-item">
      <div class="flex flex-col items-start justify-center gap-0">
        <div class="flex items-center gap-2">
          <div class="text-4xl" i-ep-element-plus style="color: #13675b"/>
          <div class="flex flex-col">
            <span class="title-chinese">育种材料大数据管理平台</span>
          </div>
        </div>
      </div>
    </el-menu-item>

    <!-- 材料数据管理子菜单 -->
    <el-sub-menu index="/MaterialDataManagement" class="menu-item compact-menu">
      <template #title>材料数据管理</template>
      <el-menu-item index="/MaterialDataManagement/MaterialMetadata">材料元数据管理</el-menu-item>
      <el-menu-item index="/MaterialDataManagement/GenotypeData">材料基因型数据管理</el-menu-item>
      <el-menu-item index="/MaterialDataManagement/PhenotypeData">材料表型数据管理</el-menu-item>
      <el-menu-item index="/MaterialDataManagement/OperationLogs">数据管理操作日志</el-menu-item>
    </el-sub-menu>

    <!-- 基因型数据分析 -->
    <el-sub-menu index="/GenotypeDataAnalysis" class="menu-item compact-menu">
      <template #title>基因型数据分析</template>
      <el-menu-item index="/GenotypeDataAnalysis/1">基因型基础统计</el-menu-item>
      <el-menu-item index="/GenotypeDataAnalysis/2">群体结构分析</el-menu-item>
      <el-menu-item index="/GenotypeDataAnalysis/3">亲缘关系分析</el-menu-item>
    </el-sub-menu>

    <!-- 表型数据管理 -->
    <el-menu-item index="/PhenotypeDataManagement" class="menu-item compact-menu">表型数据管理</el-menu-item>

    <!-- GWAS&GS分析 -->
    <el-menu-item index="/GWASAnalysis" class="menu-item compact-menu">GWAS&GS分析</el-menu-item>

    <!-- 联系我们 -->
    <el-menu-item index="/Contact" class="menu-item compact-menu">联系我们</el-menu-item>

    <!-- 用户菜单 -->
    <el-sub-menu v-if="user" index="user-menu" class="menu-item compact-menu user-menu">
      <template #title>用户：{{ user.user_name }}</template>
      <el-menu-item @click="handleLogout">注销</el-menu-item>
    </el-sub-menu>
    <el-menu-item v-else index="/Login" class="menu-item compact-menu">登录｜注册</el-menu-item>

  </el-menu>

  <el-dialog
      v-model="logoutDialogVisible"
      title="注销确认"
      width="30%"
      center
      class="logout-confirm-dialog"
  >
    <span style="display: block; text-align: center">确定要注销当前用户吗？</span>
    <template #footer>
      <div style="text-align: center">
        <el-button @click="cancelLogout">取消</el-button>
        <el-button @click="confirmLogout">确定</el-button>
      </div>
    </template>
  </el-dialog>
</template>

<style lang="scss" scoped>
.el-menu-demo {
  height: 80px;
  display: flex;
  align-items: center;
  padding: 0 10px;

  // Logo部分靠左
  .logo-item {
    margin-right: auto;
    flex-shrink: 0;
  }

  /* 菜单项基础样式 */
  .menu-item {
    flex-shrink: 1;
    min-width: auto;

    // 紧凑菜单样式
    &.compact-menu {
      :deep(.el-sub-menu__title),
      &.el-menu-item {
        padding: 0 12px;
        font-size: clamp(12px, 1.4vw, 16px); // 响应式字体大小
        line-height: 1.2;
        white-space: nowrap;
        max-width: 140px;
        min-width: 80px;

        @media (min-width: 1200px) {
          padding: 0 16px;
          font-size: 16px;
          max-width: 160px;
        }

        @media (max-width: 768px) {
          padding: 0 8px;
          font-size: 12px;
          max-width: 100px;
          min-width: 60px;
        }

        @media (max-width: 480px) {
          padding: 0 6px;
          font-size: 11px;
          max-width: 80px;
          min-width: 50px;
        }
      }
    }

    // 用户菜单特殊处理
    &.user-menu {
      :deep(.el-sub-menu__title) {
        max-width: 180px;
        overflow: hidden;
        text-overflow: ellipsis;

        @media (max-width: 768px) {
          max-width: 120px;
        }

        @media (max-width: 480px) {
          max-width: 100px;
        }
      }
    }
  }

  /* 标题样式 */
  .title-chinese {
    font-size: clamp(16px, 2vw, 36px);
    font-weight: bold;
    line-height: 1.2;
    text-align: left;
    color: #13675b;
    white-space: nowrap;

    @media (max-width: 1024px) {
      font-size: 24px;
    }

    @media (max-width: 480px) {
      font-size: 12px;
    }
  }

  /* 鼠标悬停样式 */
  :deep(.el-menu-item:hover),
  :deep(.el-sub-menu:hover .el-sub-menu__title) {
    color: #13675b !important;
    font-weight: bold !important;
    background-color: #f0f9f7;
  }

  /* 子菜单项样式 */
  :deep(.el-menu--popup) {
    .el-menu-item {
      font-size: 14px;
      padding: 8px 16px;
      min-width: 160px;

      @media (max-width: 768px) {
        font-size: 13px;
        padding: 6px 12px;
        min-width: 140px;
      }
    }
  }

  /* 激活状态样式 */
  :deep(.el-menu-item.is-active) {
    color: #13675b !important;
    font-weight: bold;
    border-bottom: 2px solid #13675b;
  }

  /* 子菜单激活状态 */
  :deep(.el-sub-menu.is-active .el-sub-menu__title) {
    color: #13675b !important;
    font-weight: bold;
    border-bottom: 2px solid #13675b;
  }
}

/* 响应式布局调整 */
@media (max-width: 1024px) {
  .el-menu-demo {
    height: 60px;
    padding: 0 8px;

    .logo-item {
      :deep(.el-menu-item-content) {
        .flex.items-center.gap-2 {
          gap: 6px !important;
        }
      }
    }
  }
}

@media (max-width: 768px) {
  .el-menu-demo {
    height: 55px;
    padding: 0 5px;

    .logo-item {
      :deep(.el-menu-item-content) {
        .text-4xl {
          font-size: 24px;
        }
      }
    }
  }
}

@media (max-width: 480px) {
  .el-menu-demo {
    height: 50px;

    .logo-item {
      :deep(.el-menu-item-content) {
        .text-4xl {
          font-size: 20px;
        }

        .flex.items-center.gap-2 {
          gap: 4px !important;
        }
      }
    }
  }
}

/* 注销确认对话框样式 */
.logout-confirm-dialog {
  :deep(.el-dialog__title) {
    color: #f56c6c;
  }

  :deep(.el-button--primary) {
    background-color: #f56c6c;
    border-color: #f56c6c;

    &:hover {
      background-color: #f78989;
      border-color: #f78989;
    }
  }
}

/* 确保菜单项在超小屏幕上也能正常显示 */
@media (max-width: 360px) {
  .el-menu-demo {
    .menu-item.compact-menu {
      :deep(.el-sub-menu__title),
      &.el-menu-item {
        padding: 0 4px;
        font-size: 10px;
        max-width: 70px;
        min-width: 45px;
      }
    }

    .title-chinese {
      font-size: 11px;
    }
  }
}
</style>